package com.project.finance.mobile.user.controller;

import com.project.finance.common.po.TbUsers;
import com.project.finance.common.resultconfig.Result;
import com.project.finance.common.resultconfig.ResultGenerator;
import com.project.finance.common.util.SMSUtils;
import com.project.finance.mobile.user.service.LoginService;
import com.project.finance.mobile.user.service.RegisterService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.crypto.hash.Hash;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @Author: Brendan Li
 * @description 注册请求处理
 * @Date: 2019/12/09/11:07
 */
@RestController
@CrossOrigin(allowCredentials ="true")
public class RegisterController {
    private static final Logger logger= LogManager.getLogger(RegisterController.class);

    public static String salt = "123qwe..";

    @Autowired
    private RegisterService registerService;
    @Autowired
    private LoginService loginService;

    /**
     *  用户注册映射
     * @param phoneNum
     * @param verifyCode
     * @param password
     * @return
     */
    @RequestMapping(value = "register",method = RequestMethod.POST)
    public Result saveUser(@RequestParam("phoneNumber") String phoneNum,
                           @RequestParam("verifyCode") String verifyCode,
                           @RequestParam("password") String password,
                           HttpSession session){

        if (!StringUtils.isEmpty(phoneNum)){ //手机号非空
            if (!StringUtils.isEmpty(password)){ //密码非空
                //判断短信验证是否相等
                String smsCode = (String) session.getAttribute(phoneNum);
                logger.info("短信验证码 : " + smsCode);
                if (verifyCode.equals(smsCode)){    //如果验证码相等
                    //对密码进行加密
                    Hash hash = new SimpleHash("MD5",password,salt,1024);
                    String passwd = hash.toBase64();
                    TbUsers tbUsers = new TbUsers();
                    tbUsers.setPhoneNumber(phoneNum);
                    tbUsers.setPassword(passwd);
                    int i = registerService.saveUser(tbUsers);
                    if (i>0){
                        return ResultGenerator.apiSuccess("注册成功");
                    }
                }
                return ResultGenerator.apiError("注册失败");
            }
            return ResultGenerator.apiError("注册失败");
        }
        return ResultGenerator.apiError("注册失败");
    }

    /**
     * 短信验证码
     * @param phoneNum
     * @param session
     * @return
     */
    @RequestMapping(value = "sendVerifyCode",method = RequestMethod.GET)
    public Result getVerifyCode(@RequestParam(value = "phoneNumber") String phoneNum,
                                HttpSession session){
        if (!StringUtils.isEmpty(phoneNum)){
            TbUsers user = loginService.getUser(phoneNum);
            if (user == null){  //当手机号没有被注册时,才可以注册
                try {
                    String smsCode = SMSUtils.sendSMS(phoneNum);//当手机号非空时候发送短信
                    logger.info("发送短信 success  ");
                    session.setAttribute(phoneNum, smsCode);
                    String  str = (String) session.getAttribute(phoneNum);
                    logger.info("-------验证码"+str);
                    return ResultGenerator.apiSuccess("发送验证码成功");
                } catch (Exception e) {
                    return ResultGenerator.apiError("发送失败");
                }
            }
            return ResultGenerator.apiError("手机号已被注册");
        }
        return ResultGenerator.apiError("发送失败");
    }

}
